package cool.mtc.minghe.web.controller.sys.role;

import cool.mtc.core.page.PageData;
import cool.mtc.minghe.data.model.base.Dict;
import cool.mtc.minghe.data.model.base.IdForm;
import cool.mtc.minghe.data.model.sys.role.dto.RoleDTO;
import cool.mtc.minghe.data.model.sys.role.dto.RoleListDTO;
import cool.mtc.minghe.data.model.sys.role.form.RoleForm;
import cool.mtc.minghe.data.model.sys.role.query.RoleQuery;
import cool.mtc.minghe.service.service.sys.role.RoleService;
import cool.mtc.minghe.web.controller.BaseController;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import lombok.RequiredArgsConstructor;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * @author 明河
 */
@Tag(name = "角色管理")
@RestController
@RequestMapping("api/sys/role")
@RequiredArgsConstructor(onConstructor = @__(@Autowired))
public class RoleController extends BaseController {
    private final RoleService roleService;

    @PreAuthorize("@ps.hasPermission('sys:role:insert')")
    @Tag(name = "角色管理")
    @Operation(summary = "新增", description = "权限：sys:role:insert")
    @PostMapping("insert")
    public long insert(@RequestBody @Validated RoleForm form) {
        return roleService.insert(form);
    }

    @PreAuthorize("@ps.hasPermission('sys:role:update')")
    @Tag(name = "角色管理")
    @Operation(summary = "更新", description = "权限：sys:role:update")
    @PostMapping("update")
    public void update(@RequestBody @Validated RoleForm form) {
        roleService.update(form);
    }

    @PreAuthorize("@ps.hasPermission('sys:role:delete')")
    @Tag(name = "角色管理")
    @Operation(summary = "删除", description = "权限：sys:role:delete")
    @PostMapping("delete")
    public void delete(@RequestBody IdForm form) {
        roleService.delete(form.getId());
    }

    @PreAuthorize("@ps.hasPermission('sys:role:read')")
    @Tag(name = "角色管理")
    @Operation(summary = "分页列表", description = "权限：sys:role:read")
    @GetMapping("list/page")
    public PageData<RoleListDTO> pageList(RoleQuery query) {
        return roleService.pageList(query);
    }

    @PreAuthorize("@ps.hasPermission('sys:role:read')")
    @Tag(name = "角色管理")
    @Operation(summary = "详情", description = "权限：sys:role:read")
    @GetMapping("info")
    public RoleDTO info(Long id) {
        return roleService.info(id);
    }

    @PreAuthorize("@ps.hasPermission('sys:role:dict')")
    @Tag(name = "角色管理")
    @Operation(summary = "字典列表", description = "权限：sys:role:dict")
    @GetMapping("list/dict")
    public List<Dict> listDict() {
        return roleService.listDict();
    }
}
